package jcommon.chap3_bean;

import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import jcommon.beans4ex.Person;

import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.beanutils.PropertyUtils;

public class Bean2Map {

	public static void main(String[] args) throws Exception {
		System.out.println("-------- Test  describe --------");
		describeDemo();

		System.out.println("-------- Test BeanMap --------");
		beanMapDemo();
	}

	private static void describeDemo() {
		Person person = SetAndGet.getExamplePersonData();
		Map mPerson = null;
		try {
			mPerson = PropertyUtils.describe(person);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		System.out.println(mPerson);
	}

	private static void beanMapDemo() {
		Person person = SetAndGet.getExamplePersonData();
		BeanMap bm = new BeanMap(person);
		System.out.println(bm);

		Set keys = bm.keySet();
		Iterator keyIterator = keys.iterator();
		while (keyIterator.hasNext()) {
			String propertyName = (String) keyIterator.next();
			System.out.println("Property: " + propertyName + ", Value: "
					+ bm.get(propertyName) + ", Type: "
					+ bm.getType(propertyName).toString());
		}
	}
}
